import { create } from 'zustand';

interface FlashState {
  // Flash相关状态
  isFlashing: boolean;
  progress: number;
  error: string | null;
  
  // Flash操作
  startFlash: () => void;
  updateProgress: (progress: number) => void;
  completeFlash: () => void;
  setError: (error: string) => void;
  clearError: () => void;
  reset: () => void;
}

const useFlashStore = create<FlashState>()((set) => ({
  // 初始状态
  isFlashing: false,
  progress: 0,
  error: null,

  // 开始刷写
  startFlash: () => {
    set({
      isFlashing: true,
      progress: 0,
      error: null
    });
  },

  // 更新进度
  updateProgress: (progress: number) => {
    set({ progress: Math.max(0, Math.min(100, progress)) });
  },

  // 完成刷写
  completeFlash: () => {
    set({
      isFlashing: false,
      progress: 100,
      error: null
    });
  },

  // 设置错误
  setError: (error: string) => {
    set({
      isFlashing: false,
      error
    });
  },

  // 清除错误
  clearError: () => {
    set({ error: null });
  },

  // 重置状态
  reset: () => {
    set({
      isFlashing: false,
      progress: 0,
      error: null
    });
  }
}));

export default useFlashStore;
